{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "infile = open(\"./raw_test.json\")\n",
    "outfile = open(\"../exact_data/test.json\", 'w')\n",
    "line = infile.readlines()\n",
    "line = [eval(ele) for ele in line]\n",
    "for ele in line:\n",
    "    triplets = list(set([(e['em1Text'], e[\"label\"], e[\"em2Text\"]) for e in ele['relationMentions']]))\n",
    "    out = {\"sentText\":ele[\"sentText\"], \"relationMentions\":[]}\n",
    "    for tri in triplets:\n",
    "        tmp = {\"em1Text\":tri[0], \"em2Text\":tri[2], \"label\":tri[1]}\n",
    "        out[\"relationMentions\"].append(tmp)\n",
    "    print(out)\n",
    "    outfile.write(json.dumps(out) + '\\n')\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'em1Text': 'Iceland',\n",
       "  'em2Text': 'Reykjavik',\n",
       "  'label': '/location/country/capital'},\n",
       " {'em1Text': 'Iceland',\n",
       "  'em2Text': 'Reykjavik',\n",
       "  'label': '/location/location/contains'},\n",
       " {'em1Text': 'Fischer',\n",
       "  'em2Text': 'Iceland',\n",
       "  'label': '/people/person/nationality'},\n",
       " {'em1Text': 'Fischer',\n",
       "  'em2Text': 'Reykjavik',\n",
       "  'label': '/people/deceased_person/place_of_death'}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = {\"sentText\": \"But that spasm of irritation by a master intimidator was minor compared with what Bobby Fischer , the erratic former world chess champion , dished out in March at a news conference in Reykjavik , Iceland .\", \"relationMentions\": [{\"em1Text\": \"Iceland\", \"em2Text\": \"Reykjavik\", \"label\": \"/location/country/capital\"}, {\"em1Text\": \"Iceland\", \"em2Text\": \"Reykjavik\", \"label\": \"/location/location/contains\"}, {\"em1Text\": \"Fischer\", \"em2Text\": \"Iceland\", \"label\": \"/people/person/nationality\"}, {\"em1Text\": \"Fischer\", \"em2Text\": \"Reykjavik\", \"label\": \"/people/deceased_person/place_of_death\"}]}\n",
    "a[\"relationMentions\"]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
